package com.cloud.logger.zchain.zchainadmin.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseCtl<T extends Serializable> {



	@Autowired
	private BaseServiceImpl baseService;

	private MyMapper mapperName;

    private void setMapper(MyMapper myMapper) {
        mapperName = myMapper;
	}



	/**
	 * @description 新增一条记录
	 * @param :map
	 */
//	@ResponseBody
//	@RequestMapping(value = "save", method = RequestMethod.POST)
//	public Map save(HttpServletRequest request, @RequestBody T entity) {
//		int code = baseService.insert(entity);
//		Map result = new HashMap();
//		if (code == 1) {
//			result.put("errorMessage", "success");
//			result.put("errCode", "8200");
//			result.put("id", code);
//			result.put("success", true);
//		} else {
//			result.put("errorMessage", "failed");
//			result.put("errCode", "8200");
//			result.put("success", false);
//		}
//		return result;
//	}

	/**
	 * 删除指定ID的记录
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Map delete(HttpServletRequest request, @PathVariable long id) {
		int code = baseService.deleteByPrimaryKey(id);
		Map result = new HashMap();
		if (code == 1) {
			result.put("errorMessage", "success");
			result.put("errCode", "8200");
			result.put("id", id);
			result.put("success", true);
		} else {
			result.put("errorMessage", "failed");
			result.put("errCode", "8200");
			result.put("success", false);
		}
		return result;
	}

	/**
	 * 修改记录
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Map update(HttpServletRequest request, @PathVariable int id, @RequestBody Map map) {
		Map result = new HashMap();
		if (!map.containsKey("id")) {
			map.put("id", id);
		}
		int code = baseService.updateByPrimaryKeySelective(map);
		if (code == 1) {
			result.put("errorMessage", "success");
			result.put("errCode", "8200");
			result.put("id", id);
			result.put("success", true);
		} else {
			result.put("errorMessage", "failed");
			result.put("errCode", "8200");
			result.put("success", false);
		}
		return result;
	}

	/**
	 * 按ID值获取记录
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Map getItem(HttpServletRequest request, @PathVariable int id) {
		Map result = new HashMap();
		Object item = baseService.selectByPrimaryKey(id);
		if (null != item) {
			result.put("errorMessage", "success");
			result.put("errCode", "8200");
			result.put("data", item);
			result.put("success", true);
		} else {
			result.put("errorMessage", "failed");
			result.put("errCode", "8200");
			result.put("success", false);
		}
		return result;
	}

	/**
	 * 按ID值获取记录
	 *
	 * @param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAll", method = RequestMethod.GET)
	public Map getAllItems(HttpServletRequest request) {
		Map result = new HashMap();
		List items = baseService.getAll();
		if (null != items) {
			result.put("errorMessage", "success");
			result.put("errCode", "8200");
			result.put("data", items);
			result.put("success", true);
		} else {
			result.put("errorMessage", "failed");
			result.put("errCode", "8200");
			result.put("success", false);
		}
		return result;
	}

}
